// pages/api/api.js
var app = getApp();
var net = require("../../utils/net.js");
var utils = require("../../utils/util.js");
var dialog = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: "",
    psw: "",
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    tipno: 0,
    posting: false,
    shop: [],
    shopids: [],
    shopindex: 0
  },
  input: function (e) {
    var value = e.detail.value + "";
    var key = e.currentTarget.id + "";
    console.log(key);

    this.setData({
      [key]: value,
    });
    if (this.data.mobile.length != "") {
      this.showtip(5, "未输入手机号")
      return;
    }
    else if (this.data.mobile.length >= 11) {
      this.showtip(5, "请输入正确的手机号");
      return;
    }
    else if (this.data.mobile.length > 1) {
      this.showtip(5, "请输入正确的手机号");
      return;
    }
    else if (this.data.psw.length != "") {
      this.showtip(5, "未输入手机号");
      return;
    }

  }, showtip(lv, msg) {
    this.setData({
      tipno: lv,
      tipmsg: msg
    })
  },
  changeShop(e) {
    this.setData({
      shopindex: e.detail.value
    })
  },
  Confirm: function () {
    this.setData({
      hidden: true
    })
  },
  
  getshoplist() {
    var that = this;
    wx.showLoading({});
    net.request({
      url: '/GetShopList',
      data: {
        mobile: that.data.mobile
      }, success(res) {
        var dt = res.data;
        if (dt.flag) {
          var ids = [], shops = [];
          for (let i = 0, len = dt.data.length; i < len; ++i) {
            ids.push(dt.data[i]["id"])
            shops.push(dt.data[i]["shop_name"])
          }
          that.setData({
            shop: shops,
            shopids: ids
          })
        } else {
          that.showtip(2, dt.msg);
        }
      }, fail() {

      }, complete() {
        wx.hideLoading();
      }
    })
  },
  reset: function () {
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
 
   
  login:function(){
    // var that = this;
    if (this.data.mobile.length > 11) {
      this.showtip(1, "请输入正确的手机号");
      return;
    }
 
   else  if (this.data.mobile.length == "") {
          this.showtip(1, "未输入手机号")
         return;
           }
      //  else if (this.data.mobile.length =1) {
      //    this.showtip(1, "请输入正确的手机号");
      //    return;
      //  }
    else if (this.data.psw.length == "") {
      this.showtip(1, "未输入密码");
      return;
    }
   
    else {
      var that = this;
      net.request({
        //上线接口地址要是https测试可以使用http接口方式
        url: '/Login',
        data: {
          mobile: that.data.mobile,
          psw: that.data.psw
        },
        success: function (res) {
          console.log(res)
          if (res.data.flag) {
            wx.setStorageSync("name", res.data.data);
            console.log(res.data.data);
            wx.switchTab({
              url: '../Monthly/Monthly',
              success: function (res) { },
              fail: function (res) { },
              complete: function (res) { },
            })
          }
        
           else {
            dialog.showDialog({
              msg: "账号或密码错误"
            });
          }
        }, fail(res) {
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    dialog = this.selectComponent("#dialog");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})